package com.divine.wn.pt.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.divine.wn.pt.persistence.UserMapper;
import com.divine.wn.pt.vo.JlVO;
import com.divine.wn.pt.vo.JtVO;
import com.divine.wn.pt.vo.UserVO;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;

	@Transactional
	public Object insertUser(UserVO user) {
		
		
		userMapper.insertUser(user);
		int user_tableid= userMapper.getKey();
		
		for (JlVO jlVO : user.getJlList()) {
			System.out.println(jlVO.getHnhy());
			
			jlVO.setUser_id(user_tableid+"");
			userMapper.insertUserJl(jlVO);
			
		}
		
		for (JtVO jtVO : user.getJtList()) {
			jtVO.setUser_id(user_tableid+"");
			userMapper.insertUserJt(jtVO);
			
		}
		
		return user_tableid;
	}
	
	
	
	
	@Transactional
	public void updateUser(UserVO user){
		userMapper.updateUser(user);
		userMapper.deleteuserJl(user.getId());
		
		userMapper.deleteuserJt(user.getId());
		
		for (JlVO jlVO : user.getJlList()) {
			jlVO.setUser_id(user.getId());
			userMapper.insertUserJl(jlVO);
			
		}
		
		for (JtVO jtVO : user.getJtList()) {
			jtVO.setUser_id(user.getId());
			userMapper.insertUserJt(jtVO);
			
		}
	}
	
	

	
	public int getdataCount(HashMap<String,Object> map) {
		return userMapper.getdataCount(map);
	}
	
	public List<HashMap<String,Object>> getdatalist(HashMap<String,Object> map) {
		return userMapper.getdatalist(map);
	}
	

	public UserVO getUser(String id) {
		UserVO user=userMapper.getUser(id);
		user.setJtList(userMapper.getUserJt(id));
		int i = user.getJtList().size();
		while (i<4) {
			user.getJtList().add(new JtVO());
			i++;
			
		}
		user.setJlList(userMapper.getUserJl(id));
		i=user.getJlList().size();
		
		while (i<4) {
			user.getJlList().add(new JlVO());
			i++;
			
		}
		return user;
		
	}
	@Transactional
	public void deleteUser(String id){
		
		userMapper.deleteUser(id);
		userMapper.deleteuserJl(id);
		userMapper.deleteuserJt(id);
	}
	
}
